<?php
/**
 * Created by PhpStorm.
 * User: UsWork-02
 * Date: 2021/4/13
 * Time: 13:48
 */

namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class News extends Controller
{

    //获取分类
    public function classify()
    {
        try{
            $table = empty($_POST['table'])?"home_classify":$_POST['table'];
            $common = new Common();
            $thisWechat = Session::get("thisWechat");
            $where['uniacid'] = $thisWechat['id'];
            if(!empty($_POST['pid'])){
                $where['pid'] = $_POST['pid'];
            }
            $list = $common->public_select($table,$where,"id,name,thumb,link,pid")->limit(3)->order("sort desc")->select();
			foreach($list as $k=>$v){
				 $list[$k]['thumb'] = empty($v['thumb']) ? "" : "https://" . $_SERVER['HTTP_HOST'] . $v['thumb'];
			}
            $black['code'] = 0;
            $black['list'] = $list;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取商户内容列表
    public function newlist(){
        try{
            $common = new Common();
            if(!empty($_POST['stores_id'])){
                $where['stores_id'] = $_POST['stores_id'];
            }
            $where['classify_id'] = $_POST['classify_id'];
            $list = $common->public_select("home_news",$where,"id,title,thumb,desc,Wdate,author,source,hits")->order("sort desc")->select();
            $black['code'] = 0;
            $black['list'] = $list;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //查看详情
    public function newdetail(){
        try{
            $common = new Common();
            $where['id'] = $_POST['id'];
            $detail = $common->public_select("home_news",$where,"id,title,thumb,desc,Wdate,author,source,hits,content")->find();
            $common->public_select("home_news",$where)->setInc("hits",1);
            $black['code'] = 0;
            $black['detail'] = $detail;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取商品列表
    public function prolist(){
        try{
            $common = new Common();
            if(!empty($_POST['stores_id'])){
                $where['stores_id'] = $_POST['stores_id'];
            }
            if(!empty($_POST["classify_id"])){
                $where['classify'] = ["like","%,".$_POST["classify_id"].",%"];
            }
            $where['status'] = 1;
            $list = $common->public_select("home_spell",$where,"id,title,thumb,validity,hits,score,stores_id")->order("sort desc")->select();
            //拿出门店信息
            foreach ($list as $k=>$v){
                $stores = $common->public_select("home_stores",['id'=>$v['stores_id']],'name,logo,phone,address')->find();
                if(!empty($stores)){
                    $list[$k]['storeNmae'] = $stores['name'];
                    $list[$k]['address'] = $stores['address'];
                    $list[$k]['phone'] = $stores['phone'];
                }
            }
            $black['code'] = 0;
            $black['list'] = $list;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //查看商品详情
    public function prodetail(){
        try{
            $common = new Common();
            $where['id'] = $_POST['id'];
            $detail = $common->public_select("home_spell",$where,"id,title,thumb,validity,hits,score,stores_id,introduce,content,images")->find();
            $detail['images'] = explode(",",$detail['images']);

            //查询门店信息
            $stores = $common->public_select("home_stores",['id'=>$detail['stores_id']],"id,name,logo,phone,images,notice,desc,appid,path,link,address,score,business")->find();
            $stores['images'] = explode(",",$stores['images']);

            $common->public_select("home_spell",$where)->setInc("hits",1);
            $black['code'] = 0;
            $black['detail'] = $detail;
            $black['stores'] = $stores;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //商品评论
    public function commentApi(){
        $common = new Common();
        $unionid = $common->common();
        try{
            $where1['id'] = $unionid;
            $fans = $common->public_select("admin_fans",$where1)->find();
            if(empty($fans)){
                throw new Exception("粉丝信息有误");
            }
            $fans_id = $fans['id'];
            $rule = [
                ["spell_id", "require", "请选择商户"],
                ["score", "require", "请输入评分"],
//                ["content", "require", "请输入内容"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            $count = $common->public_select("home_spell_comment",['fans_id'=>$fans_id,'spell_id'=>$value['spell_id']])->count("id");
            if($count>=3){
                throw new Exception("该商品同一用户只能评论3次");
            }
            $value['score'] = json_decode($value['score'],true)['value'];
            if(empty($value['score'])){
                throw new Exception("请输入评分");
            }
            $value['fans_id'] = $fans_id;
            $value['addtime'] = time();

            $common->handle("home_spell_comment",$value);
            $storesScore = $common->public_select("home_spell_comment",['spell_id'=>$value['spell_id']])->sum("score");
            $storesCount = $common->public_select("home_spell_comment",['spell_id'=>$value['spell_id']])->count("id");
            $score = $storesScore/$storesCount;
            $common->public_select("home_spell",['id'=>$value['spell_id']])->update(['score'=>$score]);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //评论列表
    public function commentList(){
        try{
            $common = new Common();
            $config = $common->selconfig();
            $page = empty($_POST['page'])?1:$_POST['page'];
            $limit = empty($_POST['limit'])?100:$_POST['limit'];
            $start = ($page-1)*$limit;
            $where['a.spell_id'] = $_POST['spell_id'];
            $where['a.status'] = 2;
            $list = Db::connect($config)->name("home_spell_comment")->alias("a")
                ->join("admin_fans b","a.fans_id = b.id")
                ->field("a.id,a.score,a.coment,b.nickname,b.avatar,a.addtime")
                ->where($where)->limit($start,$limit)->order("id desc")->select();
            foreach ($list as $k=>$v){
                $list[$k]['addtime'] = date("Y-m-d H:i:s", $v['addtime']);
            }

            $black['code'] = 0;
            $black['list'] = $list;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


}